package edu.nf.waimai.dao.item.impl;

import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.item.ItemTypeDAO;
import edu.nf.waimai.model.ltem.ItemType;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

import java.util.List;

public class ItemTypeDAOImpl implements ItemTypeDAO{
    /**
     * 添加商品类型
     * @param itemType
     * @return
     */
    @Override
    public int addItemType(ItemType itemType) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "insert into items_type(shop_id,item_type_name) values(?,?)";
        return sqlExecutor.executeUpdate(sql,itemType.getShopId(),itemType.getItemTypeName());
    }


    /**
     * 通过id删除商品类型
     * @param itemTypeId
     * @return
     */
    @Override
    public int delItemTypeById(int itemTypeId) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "delete from item_type where item_type_id = ?";
        return sqlExecutor.executeUpdate(sql,itemTypeId);
    }

    /**
     * 通过商家id查询全部商品类型
     * @return
     */
    @Override
    public List<ItemType> listItemTypesByShopId(int shopId){
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select item_type_id,shop_id,item_type_name from items_type where shop_id=?";
        BeanListHandler<ItemType> beanListHandler = new BeanListHandler<>(ItemType.class);
        return sqlExecutor.executeQuery(sql,beanListHandler,shopId);
    }
}
